/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import { Project, TaskDetails } from '@ohos/hvigor';
import fse from 'fs-extra';
import path from 'path';

import { ProjectPathInfoIml } from '../../common/iml/project-path-info-iml.js';
import { BuildArtifactConst } from '../../const/build-directory-const.js';
import { OhosLogger } from '../../utils/log/ohos-logger.js';
import { ProjectTaskService } from '../service/project-task-service.js';
import { OhosAppTask } from '../task/ohos-app-task.js';

/**
 * APP打包任务基类，（公共方法、公共属性）
 *
 * @since 2023/1/13
 */
export abstract class BasePackAppTask extends OhosAppTask {
  readonly _log = OhosLogger.getLoggerWithDurationId(BasePackAppTask.name, this.durationEvent.getId());

  readonly packageTool: string;
  readonly packInfoPath: string;
  readonly packResPath: string;
  readonly entryCardDir: string;
  readonly needPackRes: boolean;
  readonly pathInfo: ProjectPathInfoIml;

  protected constructor(project: Project, taskService: ProjectTaskService, taskDetails: TaskDetails) {
    super(project, taskService, taskDetails);
    this.packageTool = this.service.getSdkInfo().getPackageTool();
    this.pathInfo = this.service.getPathInfo();
    this.packInfoPath = path.resolve(this.pathInfo.getProjectOutputPath(), BuildArtifactConst.PACK_INFO);
    this.packResPath = path.resolve(this.pathInfo.getProjectOutputPath(), BuildArtifactConst.PACK_RES);
    this.entryCardDir = path.resolve(this.project.getNodeDir(), 'EntryCard');
    this.needPackRes = fse.existsSync(this.entryCardDir) && fse.statSync(this.entryCardDir).isDirectory();
  }

  declareExecutionTool(): string {
    return this.packageTool;
  }

  protected abstract initTaskDepends(): void;

  protected abstract doTaskAction(): void;
}
